var app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        alldetail: [], //课程详情
        courseId: 0, //课程id
        compareNum: 0, //课程比较数量
        compareshow: false, //显示课程数量
        comments: [], //评论列表
        textelllipsis: true, //多行省略  
        imgUrls: [],
        sharetip: false, //分享弹窗
        from: '',
        visitorId: '',
        relatedCourse: [],
        active_id: '', 
        active_type: '',
        showReservation: false,
        teacherId: ''
    },

    init() {
        //从老师或机构主页过来，统计访客
        var that = this;
        //课程详情
        this.get_course_detail()
        //课程详情

        //对比课程数量
        var compareNum = wx.getStorageSync('compare');
        if (compareNum) {
            this.setData({
                compareNum: compareNum.length
            })
        };
        
        //评论列表
        this.get_comment();

        //推荐课程
        this.get_related_course();
    },
    onLoad: function(options) {
        console.log('课程详情参数：', options)
        if (options.scene) {
            // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
            const scene = decodeURIComponent(options.scene).split('&')[0].split('=')[1]
            console.log(decodeURIComponent(options.scene))
            options.courseId = scene
        };

        this.setData({
            courseId: options.courseId,
            active_type: options.active_type ? options.active_type : '',
            teacherId: options.teacherId ? options.teacherId : '',
            from: options.from　? options.from: ''
        })
    },

    onShow: function() {
        this.init();
    },

    onHide: function() {
        if(this.data.from){
            app.visitorLeave(this.data.visitorId, () => {
                console.log('发送访客离开时间成功');
            });
        }
        
    },

    onUnload: function() {
        if(this.data.from){
            app.visitorLeave(this.data.visitorId, () => {
                console.log('发送访客离开时间成功');
            });
        }
    },

    onPullDownRefresh: function() {
        this.init();
    },

    onShareAppMessage: function() {
        this.setData({
            sharetip: false
        })

        let title = '这个课程不错，你快看看，跟我一起报个名？';
        if(this.data.active_type == 'crowdfunding') {
            title = this.data.alldetail.crowdfunding.title ? this.data.alldetail.crowdfunding.title : title;
        }
        return {
            title: title,
            page: '/pages/classdetail/index?courseId='+this.data.courseId+'&active_type='+this.data.active_type
        }
    },
    apply: function() { //立即报名
        var that = this;
        wx.navigateTo({
            url: '/pages/order/index?courseId=' + that.data.courseId+'&active_id='+this.data.active_id+'&active_type='+this.data.active_type,
        })
    },
    compare: function(e) { //课程对比
        var courseId = e.currentTarget.dataset.id
        var compareCourse = wx.getStorageSync('compare');
        if (compareCourse) {
            if (compareCourse.length > 4) {
                this.setData({
                    compareNum: 5
                })
                wx.showToast({
                    title: '最多同时对比5个课程哦',
                    icon: 'none'
                })
            } else {
                if (compareCourse.length == 1 && compareCourse.indexOf(courseId) >= 0) {
                    wx.showToast({
                        title: '2个以上的课程才能对比哦',
                        icon: 'none'
                    })
                };
                if (compareCourse.indexOf(courseId) < 0) {
                    compareCourse.push(courseId)
                    wx.setStorageSync('compare', compareCourse)
                    this.setData({
                        compareNum: this.data.compareNum + 1
                    })
                };

            };
        } else {
            this.setData({
                compareNum: 1
            })
            var courses = []
            courses.push(courseId)
            wx.setStorageSync('compare', courses)
            wx.showToast({
                title: '2个以上的课程才能对比哦',
                icon: 'none'
            })
        }
        this.setData({
            compareshow: true
        })
    },
    get_course_detail : function() { //获取课程详情
        var that = this;
        app.request({
            url: 'course/detail',
            data: {
                course_id: this.data.courseId,
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    that.setData({
                        alldetail: res.data,
                        imgUrls: res.data.courseImages,
                        active_id: res.data.active_id,
                        // active_type: res.data.active_type,
                    })
                    wx.hideLoading();
                    //从老师或机构主页过来，统计访客
                    if(that.data.from && !that.data.visitorId){
                        app.visitorEnter({
                            title: '课程-'+res.data.courseName,
                            schoolId: res.data.schoolId,
                            courseId: res.data.courseId,
                            teacherId: that.data.teacherId,
                            from: that.data.from
                        }, (visitorId) => {
                            that.setData({
                                visitorId: visitorId
                            });
                        });
                    }
                } else {
                    app.alert(res.msg, () => {
                        wx.navigateBack();
                    });
                }
            }
        })
    },

    goto_compare: function() {
        var compareCourse = wx.getStorageSync('compare');
        if (compareCourse.length > 1) {
            wx.navigateTo({
                url: '/pages/comparison/index',
            })
        } else {
            wx.showToast({
                title: '2个以上的课程才能对比哦',
                icon: 'none'
            })
        }

    },
    want: function() { //想学
        var that = this;
        app.request({
            url: 'course/want',
            data: {
                course_id: that.data.courseId
            },
            sCallback: (res) => {
                wx.showToast({
                    title: res.msg,
                })
            }
        })
    },
    get_comment: function() { //评论列表
        var that = this;
        app.request({
            url: 'remark/listCourse',
            data: {
                course_id: that.data.courseId
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        comments: res.data
                    })
                }
            }
        })
    },
    reply: function(e) { //回复评论
        wx.navigateTo({
            url: '/pages/replay/index?parentRemarkId=' + e.currentTarget.dataset.id + '&courseId=' + this.data.courseId,
        })

    },
    like_remark: function(e) { //评论点赞
        var that = this;
        app.request({
            url: 'remark/likeCourse',
            data: {
                remarkId: e.currentTarget.dataset.id
            },
            sCallback:(res) => {
                if(res.code ==1 ) {
                    that.get_comment();
                }
                wx.showToast({
                    title: res.msg,
                })
            }
        })

    },

    allellipsisFn: function() { //显示所有内容
        this.setData({
            textelllipsis: false
        })
    },
    ellipsisFn: function() { //隐藏所有内容
        this.setData({
            textelllipsis: true
        })
    },
    teacherme: function() { //已学
        var that = this;
        app.request({
            url: 'course/learned',
            data: {
                course_id: that.data.courseId
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    if (res.data.haveOrder) {
                        wx.navigateTo({
                            url: '/pages/scoreclass/index?courseId=' + that.data.alldetail.courseId,
                        })
                    }
                    wx.showToast({
                        title: res.msg,
                    })
                } else {
                    wx.showToast({
                        title: res.msg,
                    })
                }

                
            }
        })
        
    },
    share: function() { //分享弹框
        this.setData({
            sharetip: true
        })
    },
    share_circle: function() { //分享到朋友圈
        wx.navigateTo({
            url: '/pages/share/index?type=class',
        })
        this.setData({
            sharetip: false
        })
    },
    schoolapply: function(e) { //适用机构
        wx.navigateTo({
            url: '/pages/apply/apply?schoolBrandId=' + e.currentTarget.dataset.id + '&schoolId=' + e.currentTarget.dataset.schoolid,
        })
    },
    previewFn: function(e) { //预览图片
        wx.previewImage({
            current: e.currentTarget.dataset.url, // 当前显示图片的http链接
            urls: this.data.imgUrls // 需要预览的图片http链接列表
        })
    },
    hideMask() {
        this.setData({
            sharetip: false
        })
    },
    //打开地图
    openMap() {
        let data = this.data.alldetail;
        let latitude = parseFloat(data.lat);
        let longitude = parseFloat(data.lng);
        console.log(latitude + '--' + longitude);
        wx.openLocation({
            latitude: latitude, // 必需
            longitude: longitude, // 必需
            name: data.schoolName,
            address: data.location,
            success: (res) => {

            },
            fail: (res) => {

            },
            complete: (res) => {

            }
        })
    },

    //获取推荐课程列表
    get_related_course() {
        let that = this;
        app.request({
            url: 'course/related',
            data: {
                course_id: that.data.courseId
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        relatedCourse: res.data
                    });
                }
            }
        })

    },
    //显示预约表单
    show_reservation() {
        this.setData({
            showReservation: true
        })
    },
    hide() {
        this.setData({
            showReservation: false
        })
    },
    //显示活动详情
    changeAcitve(e){
        this.setData({
            active_type: app.getBindParam(e, 'type')
        })
    }
})